﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Backend.JYD.Model.Task;
using Backend.JYD.Business;
using Backend.JYD.Model;
using Backend.JYD.Model.Cust;

namespace Backend.JYD.BLL.Task
{
    public class IntentEntryInfoBLL
    {
        private static readonly IDAL.Task.IIntentEntryInfo idal = Factory.DALFactory.CreateEFDAL<IDAL.Task.IIntentEntryInfo>("DAL.IntentEntryInfoDAL");
        private TaskManager manager = new TaskManager();

        public Int64 AddIntentEntryInfo(IntentEntryInfo info)
        {
            if (string.IsNullOrWhiteSpace(info.CustName) || string.IsNullOrWhiteSpace(info.Phone)) return 0;
            return idal.AddIntentEntryInfo(info);
        }

        public bool UpdateIntentEntryInfo(IntentEntryInfo info)
        {
            if (info.IntentId <= 0) return false;

            bool success = idal.UpdateIntentEntryInfo(info);
            return success;
        }

        public bool NextStep(IntentEntryInfo info, string remark)
        {
            bool success = false;
            if (info.Status == 1)
            {
                success = manager.BeginProcessStep(info.TaskId, info.OpId, remark);
            }
            else if (info.Status == 2)
            {
                manager.CreateIntentTask(info, info.CustNo);
                if (info.TaskDetailId > 0 && info.TaskId > 0)
                {
                    success = manager.IntentNextStep(info.TaskId, remark, info.OpId);
                }
            }
            if (success)
            {
                info.Status = 2;
                idal.UpdateIntentEntryInfo(info);
            }
            return success;
        }

        public bool UpdateIntentEntryInfoAndNext(IntentEntryInfo info, string remark)
        {
            bool success = success = NextStep(info, remark); 
            if (success)
            {
                this.UpdateIntentEntryInfo(info);
            }
            return success;
        }

        public List<IntentEntryInfo> GetIntentEntryInfoByEmpId(Int64 empId)
        {
            if (empId <= 0) return null;
            var list = idal.GetIntentEntryInfoByEmpId(empId);
            return list == null ? null : list.ToList();
        }

        public PageInfo<IntentEntryInfo> GetIntentEntryInfoByPage(Int64 empId, DateTime? start, DateTime? end, int status, PageInfo page)
        {
            if (empId <= 0) return null;
            return idal.GetIntentEntryInfoByPage(empId, start, end, status, page);
        }

        public IntentEntryInfo GetIntentEntryInfoByIntentId(Int64 intentId)
        {
            if (intentId <= 0) return null;
            return idal.GetIntentEntryInfoByIntentId(intentId);
        }

        public IList<IntentEntryInfo> GetIntentEntryInfoByTaskIds(IList<Int64> taskIds)
        {
            if (taskIds == null || taskIds.Count <= 0) return null;
            return idal.GetIntentEntryInfoByTaskIds(taskIds);
        }

        public PageInfo<CustBase> SearchCustBaseInfo(PageInfo page, string name, string phone, int? chanel)
        {
            return idal.SearchCustBaseInfo(page, name, phone, chanel);
        }
    }
}
